package com.jiaomatech.demo.order.controller;

import com.alibaba.fastjson.JSON;
import com.jiaomatech.demo.order.dto.OperationResultDto;
import com.jiaomatech.demo.order.dto.OrderDto;
import com.jiaomatech.demo.user.dto.UserDto;
import com.jiaomatech.demo.user.feign.UserSearchFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * Description: 用户查询 .<br>
 *
 * @author m-xy
 *     Created By 2019/10/18 14:10
 */
@RestController
@RequestMapping("/order/operation")
@Api(tags = "订单信息操作服务")
public class OrderController {
    private final static Logger logger = LoggerFactory.getLogger(OrderController.class);
    @Autowired private UserSearchFeign userSearchFeign;

    /**
     * 新增购买订单 .
     *
     * @return ResponseEntity.
     */
    @RequestMapping(value = "/buy", method = RequestMethod.POST,
                    produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "新增购买订单",
                  notes = "孟祥元开发于 2019/10/18 15:50 。")
    public OperationResultDto buy(@Validated @RequestBody OrderDto orderDto) {
        ResponseEntity<UserDto> user = userSearchFeign.findUserById(orderDto.getBuyerId());
        UserDto userDto = user.getBody();
        logger.info("[当前买家的信息:{}]", JSON.toJSONString(userDto));
        if (Objects.isNull(userDto)) {
            return OperationResultDto.fail("用户不存在");
        }
        // TODO 生成对应订单
        return OperationResultDto.ok();
    }
}
